cc.Class({
    extends: cc.Component,

    properties: {
        // HistoryItemPrefab: {
        //     default: null,
        //     type: cc.Prefab,
        // },
        // foo: {
        //    default: null,
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        _history: null,
        _viewlist: null,
        _content: null,
        _viewitemTemp: null,
        _historyData: null,
        _curRoomInfo: null,
        _emptyTip: null,
        // _common: null
    },

    // use this for initialization
    onLoad: function () {

        this._history = this.node.getChildByName("history");
        this._history.active = false;

        this._emptyTip = this._history.getChildByName("emptyTip");
        this._emptyTip.active = true;

        this._viewlist = this._history.getChildByName("viewlist");
        this._content = cc.find("view/content", this._viewlist);

        this._viewitemTemp = this._content.children[0];
        this._content.removeChild(this._viewitemTemp);

        var node = cc.find("Canvas/top_right/btn_results");
        this.addClickEvent(node, this.node, "History", "onBtnHistoryClicked");

        var node = cc.find("Canvas/history/btn_back");
        this.addClickEvent(node, this.node, "History", "onBtnBackClicked");

        // this._common = this.node.getChildByName('common');
        // this._common.active = false;
    },

    addClickEvent: function (node, target, component, handler) {
        var eventHandler = new cc.Component.EventHandler();
        eventHandler.target = target;
        eventHandler.component = component;
        eventHandler.handler = handler;

        var clickEvents = node.getComponent(cc.Button).clickEvents;
        clickEvents.push(eventHandler);
    },

    onBtnBackClicked: function () {
        if (this._curRoomInfo == null) {
            this._historyData = null;
            this._history.active = false;
        }
        else {
            this.initRoomHistoryList(this._historyData);
        }
    },

    onBtnHistoryClicked: function () {
        this._history.active = true;
        var self = this;
        cc.vv.userMgr.getHistoryList(function (data) {
            if (data == null) {
                return;
            }
            data.sort(function (a, b) {
                return a.time < b.time;
            });
            self._historyData = data;
            self.initRoomHistoryList(data);
        });
    },

    dateFormat: function (time) {
        var date = new Date(time);
        var datetime = "{0}-{1}-{2} {3}:{4}:{5}";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        month = month >= 10 ? month : ("0" + month);
        var day = date.getDate();
        day = day >= 10 ? day : ("0" + day);
        var h = date.getHours();
        h = h >= 10 ? h : ("0" + h);
        var m = date.getMinutes();
        m = m >= 10 ? m : ("0" + m);
        var s = date.getSeconds();
        s = s >= 10 ? s : ("0" + s);
        datetime = datetime.format(year, month, day, h, m, s);
        return datetime;
    },

    initRoomHistoryList: function (data) {
        for (var i = 0; i < data.length; ++i) {
            var node = this.getViewItem(i);
            node.idx = i;
            var titleId = "" + (i + 1);
            var typeV = "";
            if (data[i].type == "mj") {
                typeV = "麻将";
            } else if (data[i].type == "nn") {
                typeV = "牛牛";
            } else if (data[i].type == "jdb") {
                typeV = "九点半";
            }
            for (var j = 0; j < data[i].seats.length; j++) {
                if (data[i].seats[j].userid != 0) {
                    if (data[i].seats[j].userid == cc.vv.userMgr.userId) {
                        node.getChildByName("settlement").getComponent(cc.Label).string = data[i].seats[j].score;
                    }
                }
            }
            node.getChildByName("type").getComponent(cc.Label).string = typeV;
            node.getChildByName("title").getComponent(cc.Label).string = titleId;
            node.getChildByName("roomNo").getComponent(cc.Label).string = data[i].id;
            var datetime = this.dateFormat(data[i].time * 1000);
            node.getChildByName("time").getComponent(cc.Label).string = datetime;

            var btnOp = node.getChildByName("btnOp");
            btnOp.idx = i;
            btnOp.getChildByName("Label").getComponent(cc.Label).string = "详情";
        }

        this._emptyTip.active = data.length == 0;
        this.shrinkContent(data.length);
        this._curRoomInfo = null;
    },

    initGameHistoryList: function (roomInfo, data) {
        data.sort(function (a, b) {
            return a.create_time < b.create_time;
        });
        var typeV = "";
        if (roomInfo.type == "mj") {
            typeV = "麻将";
        } else if (roomInfo.type == "nn") {
            typeV = "牛牛";
        } else if (roomInfo.type == "jdb") {
            typeV = "九点半";
        }
        console.log(roomInfo);
        console.log(data)
        var results = [];
        for (var i = 0; i < data.length; ++i) {
            var node = this.getViewItem(i);
            var idx = data.length - i - 1;
            node.idx = idx;
            var titleId = "" + (idx + 1);
            node.getChildByName("title").getComponent(cc.Label).string = i + 1;
            node.getChildByName("type").getComponent(cc.Label).string = typeV;
            node.getChildByName("roomNo").getComponent(cc.Label).string = roomInfo.id;
            var datetime = this.dateFormat(data[i].create_time * 1000);
            node.getChildByName("time").getComponent(cc.Label).string = datetime;
            var result = eval(data[i].result);
            for (var j = 0; j < result.length; j++) {
                if (result[j] != 0) {
                    if (result[j].userid == cc.vv.userMgr.userId) {
                        node.getChildByName("settlement").getComponent(cc.Label).string = result[j].score;
                    }
                }
            }
            results.unshift(result);
            var btnOp = node.getChildByName("btnOp");
            btnOp.idx = idx;
            btnOp.getChildByName("Label").getComponent(cc.Label).string = "回放";
        }
        roomInfo.results = results;
        this.shrinkContent(data.length);
        this._curRoomInfo = roomInfo;
    },

    getViewItem: function (index) {
        var content = this._content;
        if (content.childrenCount > index) {
            return content.children[index];
        }
        var node = cc.instantiate(this._viewitemTemp);
        content.addChild(node);
        return node;
    },
    shrinkContent: function (num) {
        while (this._content.childrenCount > num) {
            var lastOne = this._content.children[this._content.childrenCount - 1];
            this._content.removeChild(lastOne, true);
        }
    },

    getGameListOfRoom: function (idx) {
        var self = this;
        var roomInfo = this._historyData[idx];
        cc.vv.userMgr.getGamesOfRoom(roomInfo.uuid, function (data) {
            if (data != null && data.length > 0) {
                self.initGameHistoryList(roomInfo, data);
            }
        });
    },

    getDetailOfGame: function (idx) {
        var self = this;
        var roomUUID = this._curRoomInfo.uuid;
        cc.vv.userMgr.getDetailOfGame(roomUUID, idx, function (data) {
            data.base_info = JSON.parse(data.base_info);
            data.action_records = JSON.parse(data.action_records);
            data.results = self._curRoomInfo.results[idx];
            cc.vv.replayMgr.init(data);
            //这里判断进入的是哪个场景
            cc.vv.gameNetMgr.button = data.base_info.button;
            cc.vv.gameNetMgr.resultslist = data.action_records.infolist;
            if (data.base_info.type == "mj") {
                cc.vv.gameNetMgr.prepareMJReplay(self._curRoomInfo, data);
                cc.director.loadScene("mjgame");
            } else if (data.base_info.type == "nn") {
                cc.vv.gameNetMgr.prepareNNReplay(self._curRoomInfo, data);
                cc.director.loadScene("nngame");
            } else if (data.base_info.type == "jdb") {
                cc.vv.gameNetMgr.prepareNNReplay(self._curRoomInfo, data);
                cc.director.loadScene("jdbgame");
            }
        });
    },

    onViewItemClicked: function (event) {
        var idx = event.target.idx;
        console.log(idx);
        if (this._curRoomInfo == null) {
            this.getGameListOfRoom(idx);
        }
        else {
            this.getDetailOfGame(idx);
        }
    },

    onBtnOpClicked: function (event) {
        var idx = event.target.parent.idx;
        console.log(idx);
        if (this._curRoomInfo == null) {
            this.getGameListOfRoom(idx);
        }
        else {
            this.getDetailOfGame(idx);
        }
    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
